package ar.com.larctest.forms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ar.com.larctest.R;
import ar.com.larctest.adapters.ListViewMasterDataHorsesAdapter;
import ar.com.larctest.adapters.SpinnerHumidityAdapter;
import ar.com.larctest.adapters.SpinnerTemperatureAdapter;
import ar.com.larctest.adapters.SpinnerTrackAdapter;
import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.Horses;
import ar.com.larctest.entities.Humidity;
import ar.com.larctest.entities.Temperature;
import ar.com.larctest.entities.Track;
import ar.com.larctest.entities.temp.TMeasurement;
import ar.com.larctest.entities.temp.TMeasurementItem;
import ar.com.larctest.providers.HorsesProvider;
import ar.com.larctest.providers.HumidityProvider;
import ar.com.larctest.providers.TemperatureProvider;
import ar.com.larctest.providers.TrackProvider;
import ar.com.larctest.providers.temp.MeasurementTempProvider;
import ar.com.larctest.utils.Constants;

public class MeasurementInitActivity extends Activity {
    private static final String TAG = "MeasurementInitActivity";

    List<Track> lstTrack = new ArrayList<Track>();
    List<Temperature> lstTemperature = new ArrayList<Temperature>();
    List<Humidity> lstHumidity = new ArrayList<Humidity>();

    Spinner spiTrack;
    Spinner spiTemperature;
    Spinner spiHumidity;
    ListView listViewHorsesData;

    Track selectedTrack;
    Temperature selectedTemperature;
    Humidity selectedHumidity;

    private ListViewMasterDataHorsesAdapter horsesAdapter;
    private List<Horses> lstHorses = new ArrayList<Horses>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measurement_init);

        SQLLARCTESTHelper sqlHelper = SQLLARCTESTHelper.getHelper(this);
        sqlHelper.dbOpen();

        try
        {
            spiTrack = (Spinner) findViewById(R.id.track);
            spiTemperature = (Spinner) findViewById(R.id.temperature);
            spiHumidity = (Spinner) findViewById(R.id.humidity);

            listViewHorsesData = (ListView) findViewById(R.id.lvHorses);

            TrackProvider trackProvider = new TrackProvider(this);
            lstTrack = trackProvider.getTracks();
            SpinnerTrackAdapter trackAdapter =  new SpinnerTrackAdapter(this, android.R.layout.simple_spinner_item, lstTrack);
            spiTrack.setAdapter(trackAdapter);
            spiTrack.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
                {
                        selectedTrack = lstTrack.get(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                    selectedTrack = null;
                }
            });

            TemperatureProvider temperatureProvider = new TemperatureProvider(this);
            lstTemperature = temperatureProvider.getTemperature();
            SpinnerTemperatureAdapter temperatureAdapter =  new SpinnerTemperatureAdapter(this, android.R.layout.simple_spinner_item, lstTemperature);
            spiTemperature.setAdapter(temperatureAdapter);
            spiTemperature.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
                {
                    selectedTemperature = lstTemperature.get(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                    selectedTemperature = null;
                }
            });

            HumidityProvider humidityProvider = new HumidityProvider(this);
            lstHumidity = humidityProvider.getHumidities();
            SpinnerHumidityAdapter humidityAdapter =  new SpinnerHumidityAdapter(this, android.R.layout.simple_spinner_item, lstHumidity);
            spiHumidity.setAdapter(humidityAdapter);
            spiHumidity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
                {
                    selectedHumidity = lstHumidity.get(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                    selectedHumidity = null;
                }
            });

            HorsesProvider horsesProvider = new HorsesProvider(this);
            lstHorses = horsesProvider.getHorses();
            horsesAdapter = new ListViewMasterDataHorsesAdapter(MeasurementInitActivity.this, R.id.lvHorses, lstHorses);
            View header = getLayoutInflater().inflate(R.layout.listview_header_horses, null);
            listViewHorsesData.addHeaderView(header);

            listViewHorsesData.setAdapter(horsesAdapter);
            listViewHorsesData.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Horses selectedHorse = lstHorses.get(position - 1);
                    if(selectedHorse.getSelected())
                        selectedHorse.setSelected(false);
                    else
                        selectedHorse.setSelected(true);
                    horsesAdapter.notifyDataSetChanged();
                }
            });


        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        finally{
            sqlHelper.dbClose();
        }
    }

    public void onClickStart(View view)
    {
        try
        {
            Boolean selectAnyHorse = false;
            for(Horses horseItem: lstHorses) {
                if(horseItem.getSelected()) {
                    selectAnyHorse = true;
                    break;
                }
            }
            if(selectedTrack == null) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText(getText(R.string.Measurement_Init_msgInputTrack));

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
            else if(selectedTemperature == null) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText(getText(R.string.Measurement_Init_msgInputTemperature));

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
            else if(selectedHumidity == null) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText(getText(R.string.Measurement_Init_msgInputHumidity));

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
            else if(!selectAnyHorse) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText(getText(R.string.Measurement_Init_msgInputHorses));

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
            else
            {
                MeasurementTempProvider tempProvider = new MeasurementTempProvider(this);
                tempProvider.removeMeasurement();
                tempProvider.removeMeasurementItems();

                TMeasurement measurement = new TMeasurement();
                measurement.setIdTrack(selectedTrack.getIdTrack());
                measurement.setIdTemperature(selectedTemperature.getIdTemperature());
                measurement.setIdHumidity(selectedHumidity.getIdHumidity());

                tempProvider.insertOrUpdateMeasurement(measurement);

                for(Horses horseItem: lstHorses) {
                    if(horseItem.getSelected()) {
                        TMeasurementItem measurementItem = new TMeasurementItem();
                        measurementItem.setIdHorse(horseItem.getIdHorse());
                        measurementItem.setNameHorse(horseItem.getName());
                        measurementItem.setState(Constants.STATE_INIT);
                        measurementItem.setInitTime(null);
                        measurementItem.setFinishTime(null);
                        measurementItem.setTimeOut1(Constants.INIT_TIMEOUT_1);
                        measurementItem.setTimeOut2(Constants.INIT_TIMEOUT_2);
                        measurementItem.setTimeOut3(Constants.INIT_TIMEOUT_3);
                        measurementItem.setLacticAcid1(0D);
                        measurementItem.setHeartRate1(0D);
                        measurementItem.setLacticAcid2(0D);
                        measurementItem.setHeartRate2(0D);
                        measurementItem.setLacticAcid3(0D);
                        measurementItem.setHeartRate3(0D);
                        measurementItem.setRespiratoryRate3(0D);
                        measurementItem.setSelected(false);

                        tempProvider.insertMeasurementItem(measurementItem);
                    }
                }

                Intent intentMeasurement = new Intent(this, MeasurementActivity.class);
                startActivityForResult(intentMeasurement, 1);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
}
